<template>
	<div>
		<!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>用户管理</el-breadcrumb-item>
			<el-breadcrumb-item>用户列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片视图 -->
		<el-card>
			<!-- 添加角色按钮区域 -->
			<el-row>
				<el-col>
					<el-button type="primary" @click="openAddDialog">添加角色</el-button>
				</el-col>
			</el-row>
			<!-- 角色列表区域 -->
			<el-table :data="rolelist" border stripe>

				<!-- 索引列 -->
				<el-table-column type="index"></el-table-column>
				<el-table-column label="角色名称" prop="Id"></el-table-column>
				<el-table-column label="角色名称" prop="Name"></el-table-column>
				<el-table-column label="操作" width="300px">
					<template slot-scope="scope">
						<el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑
						</el-button>
						<el-button size="mini" type="danger" @click="delRole(scope.row)" icon="el-icon-delete">删除
						</el-button>
						<el-button size="mini" type="warning" icon="el-icon-setting"
							@click="showSetRightDialog(scope.row)">分配权限</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<!-- 分配权限的对话框 -->
		<el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="serRightDialogClosed">
			<!-- 树形控件 -->
			<el-tree :data="rightslist" :props="treeProps" show-checkbox node-key="Id" :default-expand-all="false"
				:default-checked-keys="defKeys" ref="treeRef"></el-tree>
			<span slot="footer" class="dialog-footer">
				<el-button @click="setRightDialogVisible = false">取 消</el-button>
				<el-button type="primary" @click="allotRights">确 定</el-button>
			</span>
		</el-dialog>








		<!-- 添加角色对话框 -->
		<el-dialog title="收货地址" :visible.sync="AddDialogFormVisible">
			<el-form :model="AddForm">
				<el-form-item label="角色名称" label-width="100px">
					<el-input v-model="AddForm.name" autocomplete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="AddDialogFormVisible = false">取 消</el-button>
				<el-button type="primary" @click="addRole">确 定</el-button>
			</div>
		</el-dialog>

		<!-- 编辑对话框 -->
		<el-dialog title="编辑" :visible.sync="editdialogVisible" width="30%">
			<el-form ref="editform" :model="editform" label-width="80px">
				<el-form-item label="角色名">
					<el-input v-model="editform.Name"></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="resetForm()">重置</el-button>
				<el-button type="primary" @click="tijiao">确 定</el-button>
			</span>
		</el-dialog>










	</div>
</template>
<script>
	export default {
		data() {
			return {
				//编辑对话框显示与隐藏
				editdialogVisible: false,
				AddForm: {
					name: ""
				},
				//添加角色对话框显示与隐藏
				AddDialogFormVisible: false,
				//所有角色列表数据
				rolelist: [],
				//控制分配权限对话框的显示与隐藏
				setRightDialogVisible: false,
				//所有权限的数据
				rightslist: [],
				//树形控件的属性绑定对象
				treeProps: {
					label: 'Title',
					children: 'Children',
				},
				//默认选中的节点Id值数组
				defKeys: [5, 1],
				//当前  分配权限的角色id
				roleName: '',
				//修改内容
				editform: {},
			}
		},
		created() {
			this.getRolesList()
		},
		methods: {
			//获取所有角色的列表
			async getRolesList() {
				var userId = window.sessionStorage.getItem('userId');
				const {
					data: res
				} = await this.$http.post('/api/api/getRolesList', {
					userId: userId
				});
				console.log("角色列表res")
				console.log(res)
				var rolesName = res.data;
				this.rolelist=[];
				rolesName.forEach((item) => {
					this.rolelist.push({
						Id:item.RoleId,
						Name: item.roleName
					})
				})

			},
			
			//展示分配权限的对话框
			async showSetRightDialog(role) {
				//角色Id
				this.roleName = role.Name
				//获取所有模块的数据
				const {
					data: res
				} = await this.$http.post(
					'/api/api/GetModulesAll', {})
				if (res.success !== true) {
					return this.$message.error('获取权限数据失败!')
				}
				//把获取到的权限数据保存到this.rightslist 中
				this.rightslist = res.data
				this.GetModulesByRoleId();


				this.setRightDialogVisible = true

			},
			//根据角色id 获取模块
			async GetModulesByRoleId() {
				const {
					data: res
				} = await this.$http.post(
					"/api/api/GetModulesByRoleIdLayUI", {
						roleName: this.roleName
					}
				);
				if (!res.success) {
					return this.$message.error('获取角色权限数据失败!')
				}
				this.$message.success('获取角色权限数据成功!')
				res.data.forEach((item) => {
					this.defKeys.push(item.ModuleId)
				})
				console.log(this.defKeys)

			},
			//监听分配权限对话框的关闭事件
			serRightDialogClosed() {
				this.defKeys = []
			},
			//点击为角色分配权限
			async allotRights() {
				const keys = [
					...this.$refs.treeRef.getCheckedKeys(),
					...this.$refs.treeRef.getHalfCheckedKeys(),
				]
				console.log("keys")
				console.log(keys)
				const idStr = keys.join(',')
				console.log(idStr)
				const {
					data: res
				} = await this.$http.post(
					"/api/api/SetSysRights", {
						rName: this.roleName,
						parentIds: idStr,
						ModuleIds: keys
					}
				)
				if (res.success !== true) {
					return this.$message.error('分配权限失败!')
				}
				this.$message.success('分配权限成功!')
				this.getRolesList()
				this.setRightDialogVisible = false
			},
			//通过递归的形式，获取角色下所有三级权限的id，并保存到defKeys数组中
			getLeafKeys(node, arr) {
				//如果当前node节点不包含children属性，则是三级节点

				if (!node.children) {

					if (node.checked) {
						return arr.push(node.id)
					}
					return
				}
				node.children.forEach((item) => this.getLeafKeys(item, arr))
			},


			//打开添加角色对话框
			openAddDialog() {
				this.AddDialogFormVisible = true;
			},
			//添加角色
			async addRole() {
				console.log("添加角色");
				const {
					data: res
				} = await this.$http.post("/api/api/CreateRole", {
					Name: this.AddForm.name

				});
				if (!res.success) {
					this.$message.error("添加角色失败");
				}
				this.$message.success("添加角色成功");
				this.AddDialogFormVisible = false;
				this.getRolesList();

			},
			//删除角色
			async delRole(obj) {
				console.log("删除角色");
				console.log(obj);
				const confirmResult = await this.$confirm(
					'此操作将永久删除该角色, 是否继续?',
					'提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning',
					}
				).catch((err) => err)
				if (confirmResult !== 'confirm') {
					return this.$message.info('取消了删除!')
				}
				const {data:res} = await this.$http.post("/api/api/DeleteRole", {
					roleId: obj.Id
				});
				if (!res.success ) {
					this.$message.error("删除角色失败");
				}
				this.$message.success("删除角色成功");

				this.getRolesList();

			},

			//编辑视图
			async edit(jsdata) {
				console.log("jsdata")
				console.log(jsdata)
				//console.log(id);
				this.editdialogVisible = true
				//console.log(jsdata)
				this.editform = jsdata
				//console.log(this.editform)
			},
			//编辑保存
			async tijiao() {
				console.log("this.editform")
				console.log(this.editform)
				const {data:res} = await this.$http.post(
					'/api/api/UpdateRole', {
						Id: this.editform.Id,
						Name: this.editform.Name
					}
				)
				console.log("更新角色信息res")
				console.log(res)
				//console.log(res);
				if(!res.success){
					return this.$message.error('更新角色失败！' + res.message)
				}
			
					this.resetForm()
					this.editdialogVisible = false
					return this.$message.success('更新角色成功！')
				
			},
			//重置方法
			resetForm() {
			  (this.addform = {}), (this.editform = {})
			},





		}
	}
</script>
<style lang="less" scoped>
	.el-tag {
		margin: 7px;
	}

	.bdtop {
		border-top: 1px solid #eee;
	}

	.bdbottom {
		border-bottom: 1px solid #eee;
	}

	.vcenter {
		display: flex;
		align-items: center;
	}
</style>
